<template>
  <div class="app-container">

    <div class="main-container">
      <!-- 侧边栏菜单 -->
      <el-aside width="220px" class="sidebar">
        <el-menu
            :default-active="activeMenu"
            class="side-menu"
            background-color="#304156"
            text-color="#bfcbd9"
            active-text-color="#409EFF"
            :collapse="isCollapse"
            router>
          <template v-for="item in menuList">
            <!-- 一级菜单 -->
            <el-submenu
                v-if="item.children && item.children.length > 0"
                :key="item.path"
                :index="item.path">
              <template slot="title">
                <i :class="item.icon"></i>
                <span>{{ item.title }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                  v-for="child in item.children"
                  :key="child.path"
                  :index="child.path">
                <i :class="child.icon"></i>
                <span>{{ child.title }}</span>
              </el-menu-item>
            </el-submenu>

            <!-- 没有子菜单的一级菜单 -->
            <el-menu-item
                v-else
                :key="item.path"
                :index="item.path">
              <i :class="item.icon"></i>
              <span>{{ item.title }}</span>
            </el-menu-item>
          </template>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-main class="content">
        <router-view></router-view>
      </el-main>
    </div>
  </div>
</template>




<script>
export default {
  name: 'MainLayout',
  data() {
    return {
      isCollapse: false,
      activeMenu: '',
      menuList: [] // 初始为空数组，将从后端获取
    }
  },
  created() {

    this.fetchMenuData();
    this.activeMenu = this.$route.path;
  },
  methods: {
    // 从后端获取菜单数据
    async fetchMenuData() {
      try {
        // const response = await this.$http.get('/api/menu/list');


        const responseStr = "{\"code\":200,\"data\":[{\"id\":1,\"parentId\":0,\"path\":\"/dashboard\",\"title\":\"控制台\",\"icon\":\"el-icon-s-home\",\"children\":[]},{\"id\":2,\"parentId\":0,\"path\":\"/system\",\"title\":\"系统管理\",\"icon\":\"el-icon-s-tools\",\"children\":[{\"id\":3,\"parentId\":2,\"path\":\"/system/user\",\"title\":\"用户管理\",\"icon\":\"el-icon-user\"},{\"id\":4,\"parentId\":2,\"path\":\"/system/role\",\"title\":\"角色管理\",\"icon\":\"el-icon-s-custom\"}]}]}" ;

        const response = JSON.parse(responseStr);

        console.log(response);


        this.menuList = this.transformMenuData(response.data);

        console.log(JSON.stringify(this.menuList));
    /*    if (response.data.code === 200) {
          this.menuList = this.transformMenuData(response.data.data);
        }*/
      } catch (error) {
        console.error('获取菜单数据失败:', error);
        this.$message.error('获取菜单数据失败');
      }
    },

    // 转换菜单数据结构（如果需要）
    transformMenuData(menuData) {
      // 这里可以根据需要转换数据结构
      // 如果后端返回的数据已经是前端需要的格式，可以直接返回
      return menuData;

      /* 如果后端返回的是扁平结构，可以使用以下代码转换为树形结构
      const tree = [];
      const map = {};

      menuData.forEach(item => {
        map[item.id] = { ...item, children: [] };
      });

      menuData.forEach(item => {
        if (item.parentId === 0) {
          tree.push(map[item.id]);
        } else {
          map[item.parentId].children.push(map[item.id]);
        }
      });

      return tree;
      */
    }
  },
  watch: {
    $route(to) {
      this.activeMenu = to.path;
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  height: 100vh;
  display: flex;
  flex-direction: column;

  .header {
    background-color: #545c64;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;

    .logo {
      font-size: 20px;
      font-weight: bold;
    }

    .top-menu {
      border-bottom: none;
    }
  }

  .main-container {
    display: flex;
    flex: 1;
    overflow: hidden;

    .sidebar {
      background-color: #304156;
      height: 100%;

      .side-menu {
        height: 100%;
        border-right: none;

        &:not(.el-menu--collapse) {
          width: 220px;
        }
      }
    }

    .content {
      padding: 20px;
      background-color: #f0f2f5;
      overflow-y: auto;
    }
  }
}

// 菜单项悬停效果
.el-menu-item:hover,
.el-submenu__title:hover {
  background-color: #263445 !important;
}

// 子菜单项样式
.el-menu--inline {
  background-color: #1f2d3d !important;

  .el-menu-item {
    padding-left: 50px !important;
  }
}
</style>